import express from "express"
import "dotenv/config"
import session from 'express-session'
import memorystore from "memorystore"



//const MemoryStore = require('memorystore')(session)

import router from "./routes/web.js"



const app = express()

const MemoryStore = memorystore(session)

app.use(session({
    cookie: { maxAge: 86400000 },
    store: new MemoryStore({
      checkPeriod: 86400000 // prune expired entries every 24h
    }),
    resave: false,
    secret: 'fhweuqrf897324t9hgoerh98p'
}))

// 把一些变量绑定到响应对象 res, 便于在视图中引用
app.use((req,res,next)=>{
    res.locals.user = req.session.user ?? null
    res.locals.session = session ?? null
    next()
})

app.use(express.static("public"))

app.set("view engine","ejs")
app.set("views","./app/views")
app.use(express.json())
app.use(express.urlencoded({extended: true}))

app.use(router)

app.listen(process.env.PORT,()=>{
    console.log(`Server is listening on port ${process.env.PORT} ...`);  
})